package com.stone.framework.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import com.stone.framework.core.context.TokenContext;

import reactor.core.publisher.Mono;

@Component
public class TokenGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getHeaders().getFirst("token");
        TokenContext.build().set(token);
        return chain.filter(exchange).then(Mono.fromRunnable(() -> TokenContext.build().remove()));
    }

    @Override
    public int getOrder() {
        return -40;
    }

}
